# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=kicad
pkgbase=mingw-w64-${_realname}-git
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}-git"
pkgver=r10561.a552b4ccc
pkgrel=1
pkgdesc="Software for the creation of electronic schematic diagrams and printed circuit board artwork (mingw-w64)"
arch=('any')
url="http://www.kicad-pcb.org"
license=("GPL2+")
provides=("${MINGW_PACKAGE_PREFIX}-${_realname}")
conflicts=("${MINGW_PACKAGE_PREFIX}-${_realname}")
depends=("${MINGW_PACKAGE_PREFIX}-boost"
         "${MINGW_PACKAGE_PREFIX}-cairo"
         "${MINGW_PACKAGE_PREFIX}-glew"
         "${MINGW_PACKAGE_PREFIX}-ngspice"
         "${MINGW_PACKAGE_PREFIX}-oce"
         "${MINGW_PACKAGE_PREFIX}-openssl"
         "${MINGW_PACKAGE_PREFIX}-wxPython"
         "${MINGW_PACKAGE_PREFIX}-wxWidgets")
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-doxygen"
             "${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-glm"
             "${MINGW_PACKAGE_PREFIX}-python2"
             "${MINGW_PACKAGE_PREFIX}-pkg-config"
             "${MINGW_PACKAGE_PREFIX}-swig"
             "git")
source=("${_realname}"::"git+https://github.com/KiCad/kicad-source-mirror.git"
        #"${_realname}-doc"::"git+https://github.com/KiCad/kicad-doc.git"
        "${_realname}-i18n"::"git+https://github.com/KiCad/kicad-i18n.git"
        "${_realname}-libs"::"git+https://github.com/KiCad/kicad-library.git"
        "${_realname}-footprints"::"git+https://github.com/KiCad/kicad-footprints.git"
        "${_realname}-symbols"::"git+https://github.com/KiCad/kicad-symbols.git"
        "${_realname}-templates"::"git+https://github.com/KiCad/kicad-templates.git")
sha256sums=('SKIP'
            'SKIP'
            'SKIP'
            'SKIP'
            'SKIP'
            'SKIP')

pkgver() {
  cd "${srcdir}/${_realname}"
  printf "r%s.%s" "$(git rev-list --count --first-parent HEAD)" "$(git rev-parse --short HEAD)"
}

prepare() {
  cp -f ${srcdir}/${_realname}/template/kicad.pro ${srcdir}/kicad-templates/
}

build() {
  cd "${srcdir}"

  # Configure and build KiCad.
  [[ -d build-${MINGW_CHOST} ]] && rm -r build-${MINGW_CHOST}
  mkdir build-${MINGW_CHOST} && cd build-${MINGW_CHOST}

  declare -a _btype
  if check_option "debug" "y"; then
    _btype=Debug
  else
    _btype=Release
  fi


  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -G"MSYS Makefiles" \
    -DCMAKE_PREFIX_PATH=${MINGW_PREFIX} \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DCMAKE_BUILD_TYPE=${_btype} \
    -DDEFAULT_INSTALL_PATH=${MINGW_PREFIX} \
    -DOPENSSL_ROOT_DIR=${MINGW_PREFIX} \
    -DKICAD_SCRIPTING=ON \
    -DKICAD_SCRIPTING_MODULES=ON \
    -DKICAD_SCRIPTING_WXPYTHON=ON \
    -DPYTHON_EXECUTABLE=${MINGW_PREFIX}/bin/python2.exe \
    ../${_realname}

  make

  cd "${srcdir}"
  # Configure the translationn installation build.
  [[ -d build-i18n ]] && rm -r build-i18n
  mkdir build-i18n && cd build-i18n
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -G "MSYS Makefiles" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    ../${_realname}-i18n

  cd "${srcdir}"
  # Configure the library installation build.
  [[ -d build-libs ]] && rm -r build-libs
  mkdir build-libs && cd build-libs
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -G "MSYS Makefiles" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    ../${_realname}-libs

  cd "${srcdir}"
  # Configure the footprints installation build.
  [[ -d build-footprints ]] && rm -r build-footprints
  mkdir build-footprints && cd build-footprints
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -G "MSYS Makefiles" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    ../${_realname}-footprints

  cd "${srcdir}"
  # Configure the templates installation build.
  [[ -d build-templates ]] && rm -r build-templates
  mkdir build-templates && cd build-templates
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -G "MSYS Makefiles" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    ../${_realname}-templates

  cd "${srcdir}"
  # Configure the symbols installation build.
  [[ -d build-symbols ]] && rm -r build-symbols
  mkdir build-symbols && cd build-symbols
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -G "MSYS Makefiles" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    ../${_realname}-symbols
}

package() {
  # Install KiCad.
  cd "${srcdir}/build-${MINGW_CHOST}"
  make DESTDIR=${pkgdir} install

  # Install KiCad i18n.
  cd "${srcdir}/build-i18n"
  make DESTDIR=${pkgdir} install

  # Install KiCad libraries.
  cd "${srcdir}/build-libs"
  make DESTDIR=${pkgdir} install

  # Install KiCad footprints.
  cd "${srcdir}/build-footprints"
  make DESTDIR=${pkgdir} install

  # Install KiCad symbols.
  cd "${srcdir}/build-symbols"
  make DESTDIR=${pkgdir} install

  # Install KiCad templates.
  cd "${srcdir}/build-templates"
  make DESTDIR=${pkgdir} install

  # Install the pretties
  #mkdir -p "${pkgdir}${MINGW_PREFIX}/share/kicad/modules"
  #cp -rd "${srcdir}/kicad-footprints"/* "${pkgdir}${MINGW_PREFIX}/share/kicad/modules"
}
